﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

using System.Text.RegularExpressions;
using System.Numerics;

namespace Xuhengxiao.Math
{
    /// <summary>
    /// 这个是一个词类，包含
    /// 词类型
    /// 值 ， 对数值类型而言就是数值，对运算符而言是字符串
    /// 优先级
    /// 目数
    /// 正则表达式匹配，用于匹配是否是这个
    /// 委托，后缀表达式中计算用
    /// </summary>
    class ClsPhrase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PhraseType1">词类型</param>
        /// <param name="StrRegex1">匹配</param>
        /// <param name="OperatorPriority1">优先级</param>
        /// <param name="OperandType1">目数</param>
        /// <param name="Value1">值</param>
        /// <param name="Calculate1">委托</param>
        public ClsPhrase(enumPhraseType PhraseType1,
            string StrRegex1, 
            int OperatorPriority1, 
            int OperandType1, 
            object  Value1, 
            p_calculate Calculate1)
        {
            PhraseType = PhraseType1;
            StrRegex = StrRegex1;
            OperatorPriority = OperatorPriority1;
            OperandType = OperandType1;
            Value = Value1;
            Calculate = Calculate1;

        }

        /// <summary>
        /// 深度拷贝
        /// </summary>
        /// <returns></returns>
        public ClsPhrase DeepCopy()
        {
            return new ClsPhrase(PhraseType, StrRegex, OperatorPriority, OperandType, Value, Calculate);
        }
        //
        private enumPhraseType _PhraseTypee;
        //
        /// <summary>
        /// 词类型
        /// </summary>
        public enumPhraseType PhraseType
        {
            get { return _PhraseTypee; }
            set { _PhraseTypee = value; }
        }

        private object  _value;
        //
        /// <summary>
        /// 值：对数值类型而言就是数值，对运算符而言是字符串
        /// </summary>
        public object  Value
        {
            get { return _value; }
            set { _value = value; }
        }

        private int _OperatorPriority;
        /// <summary>
        /// 优先级
        /// </summary>
        public int OperatorPriority
        {
            get { return _OperatorPriority; }
            set { _OperatorPriority = value; }
        }

        private int _OperandType;
        /// <summary>
        /// 目数
        /// </summary>
        public int OperandType
        {
            get { return _OperandType; }
            set { _OperandType = value; }
        }

        private string _strRegex;
        /// <summary>
        /// 正则表达式匹配，用于匹配是否是这个
        /// </summary>
        public string StrRegex
        {
            get { return _strRegex; }
            set { _strRegex = value; }
        }

        public override string ToString()
        {
            if (PhraseType==enumPhraseType.number)
            {
                return Value.ToString();
            }
            return StrRegex;
            //return base.ToString();
        }

        /// <summary>
        /// 定义一个委托，用于计算,参数和结果都是复数
        /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <returns></returns>
        public delegate Complex p_calculate(Complex c1, Complex c2);

        private p_calculate _Calculate;
        /// <summary>
        /// 这个是一个委托，用于计算的,参数和结果都是复数
        /// </summary>
        public p_calculate Calculate
        {
            get { return _Calculate; }
            set { _Calculate = value; }
        }

        //如下是方法
        public bool isMatch(string str)
        {
            return (new Regex (StrRegex)).IsMatch(str);
            //return false;
        }

        public Complex calculate(Complex c1, Complex c2)
        {
            if (Calculate != null)
            {
                return Calculate(c1, c2);//如果有定义运算就计算。

            }
            else
            {
                throw new Exception(" 没有定义这个\""+StrRegex+"\"的实现代码");
            }

        }


    }
}
